Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
How does the "this" keyword works in these particular cases?

Suppose we have the following Javascript object:

const myObj2 = {
    name: "my object",
    thisOne: this,
    print1: function() {
        console.log("(1)", this.thisOne);
        console.log("(2)", this);
        
        const innerFuncDecl = function() {
            console.log("(3)", this);
        };
        
        const innerFuncArrow = () => {
            console.log("(4)", this);
        };

        innerFuncDecl();
        innerFuncArrow();

        (function(){
            console.log("(5)", this);
        })();

        (() => {
            console.log("(6)", this);
        })();
    },

    print2: () => {
        console.log("(7)", this);
    },

    print3: function(){
        console.log("(8)", this);
    },
};
    

If I call print1, print2 and print3 from my browser I have:

enter image description here

Questions:

  • Why (1) "this" points to "Window" and not to "myObj2"?
  • Why (2) and (8) "this" point to "myObj2", while (7) "this" points to "Window"?
  • Why (3) "this" points to "Window"? Is it because it's a function declaration, and function declarations are always hoisted to the global context? Same for (5)?
  • Why (4) "this" points to "myObj2"? Is it because it's a function expression, and function expression are never hoisted and maintain the context of the enclosing function? Same for (6)
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda